# -*- coding: utf-8 -*-

def mycompare(x,y):
    if x.length == y.length:
        if x.name < y.name:
            return 1
        else:
            return -1
    else:
        return x.length - y.length
class Stu:
    def __init__(self, name, length):
        self.name = name
        self.length = length


N,K = map(int, raw_input().strip().split(' '))

Stus = []

#Stus.append(Stu('Tom', 188))
#Stus.append(Stu('Mike', 170))
#Stus.append(Stu('Eva', 168))
#Stus.append(Stu('Tim', 160))
#Stus.append(Stu('Joe', 190))
#Stus.append(Stu('Ann', 168))
#Stus.append(Stu('Bob', 175))
#Stus.append(Stu('Nick', 186))
#Stus.append(Stu('Amy', 160))
#Stus.append(Stu('John', 159))

i=0
while i<N:
    name, length=raw_input().split(' ')
    length = int(length)
    Stus.append(Stu(name, length))
    i+=1

Stus = sorted(Stus, cmp=mycompare)
#for e in Stus:
#    print e.name,e.length

lines = []
lines.append([0]*(N/K+N%K))
for i in range(1, K):
    lines.append([0]*(N/K))
    
for line in lines:
    m=len(line)
    line[m/2]=Stus.pop().name
    symbol=-1
    k=0
    t_count=0
    #使用 line.count(0)的话也可以，但是会超时
    while t_count<m-1:
        if symbol<0:
            k+=1
        line[m/2+symbol*k]=Stus.pop().name
        symbol*=-1
        t_count+=1

for line in lines:
    print ' '.join(line)